<?php

function _wpud_check($plugin_id)
{
  $wpud_check = _wpud_get_plugin_module($plugin_id, "settings", true);
  if ($wpud_check)
  {
    _wpud_plugin_load($plugin_id);
  }
  return $wpud_check;
}

function _wpud_get_plugins()
{
  $plugin_ids = array();
  $dirplugin = rtrim(dirname(__FILE__), "/")."/plugins/";
  $diretorio = $dirplugin;
  $ponteiro  = opendir($diretorio);
  $itens = array();
  while ($nome_itens = readdir($ponteiro))
  {
    $itens[] = $nome_itens;
  }
  sort($itens);
  foreach ($itens as $listar)
  {
    if ( $listar!="." && $listar!=".." && $listar!=".svn" )
    {
      if ( is_dir("$diretorio/$listar") )
      {
        $plugin_ids[] = $listar;
      }
    }
  }
  return $plugin_ids;
}

function _wpud_get_plugin_module($plugin_id, $module, $return_bolean = false)
{
  $file = "_wpud_".$plugin_id."_".$module.".php";
  $dir = rtrim(dirname(__FILE__), "/")."/plugins/".trim($plugin_id, "/");
  $full_file = "$dir/$file";
  if (@file_exists($full_file))
  {
    if ($return_bolean)
    {
      return true;
    }
    else
    {
      include_once $full_file;
    }
  }
  else
  {
    $msg_error = __("File needed module WP User Data does not exist", "wpud")." (".$full_file.")";
    if ($return_bolean)
    {
      echo "<script type=\"text/javascript\">alert('".$msg_error."');</script>";
      return false;
    }
    else
    {
      echo $msg_error;
    }
  }
}

function _wpud_plugin_load($plugin_id)
{
  _wpud_get_plugin_module($plugin_id, "functions");
}

function _wpud_register_script($plugin_id)
{
  $file = "_wpud_".$plugin_id."_"."functions.js";
  $dir = rtrim(dirname(__FILE__), "/")."/plugins/".trim($plugin_id, "/");
  $full_file = "$dir/$file";
  if (@file_exists($full_file))
  {
    wp_register_script("_wpud_".$plugin_id."_js", rtrim(WP_PLUGIN_URL, "/")."/wp-user-data/plugins/".trim($plugin_id, "/")."/$file");
    wp_enqueue_script("_wpud_".$plugin_id."_js");
  }
}

function _wpud_get_plugin_name($plugin_id)
{
  $function_name = "_wpud_get_plugin_name_".$plugin_id;
  if (function_exists($function_name))
  {
    return call_user_func($function_name);
  }
  else
  {
    $plugin_name = "WordPress Plugin";
    return  $plugin_name;
  }
}

function _wpud_option_enable($option, $bolean_true = "on")  /** [byAZM] function _wpud_option_enable - Begin */
{
		$isItEnabled = get_option($option);
		if (empty($isItEnabled))
		{
			//enabling option for the first time.
			add_option($option, $bolean_true);
		}
		else
		{
			delete_option($option);
			add_option($option, $bolean_true);
		}
}    /** [byAZM] function _wpud_option_enable - End */

function _wpud_option_disable($option, $bolean_true = "on", $bolean_false = "off")  /** [byAZM] function _wpud_option_disable - Begin */
{
		$currentStatus = get_option($option);
		//if option is already off then do nothing.
		if ($currentStatus == $bolean_false)
		{
			 return false;
		}
		//if option serise is on. then turn it off
		if ($currentStatus == $bolean_true)
		{
			delete_option($option);
			add_option($option, $bolean_false);
		}
		//if for some reason the option is missing, then create it and then set it to off.
		if (empty($currentStatus))
		{
			add_option($option, $bolean_false);
		}
		return true;
}  /** [byAZM] function _wpud_option_disable - End */

function wpud_get_availables_data($module = false)
{
	#--
	$prefix = "wpud"."_";
	#--
	$modules = array();
	$modules [] = "profile_main";
	$modules [] = "mail_main";
	$modules [] = "business_main";
	$modules [] = "customer_main";
	$modules [] = "tel_phone";
	$modules [] = "tel_mobile";
	$modules [] = "tel_fax";
	$modules [] = "social_facebook";
	$modules [] = "pay_paypal";
	#--
	$fields = array();
	foreach ($modules as $value)
	{
		$fields[$value] = array();
	}
	#--
	$fields["profile_main"][] = "sex";
	$fields["profile_main"][] = "birth_date";
	#--
	$fields["mail_main"][] = "country";
	$fields["mail_main"][] = "region";
	$fields["mail_main"][] = "city";
	$fields["mail_main"][] = "zipcode";
	$fields["mail_main"][] = "full_address";
	$fields["mail_main"][] = "street_address";
	$fields["mail_main"][] = "number_address";
	$fields["mail_main"][] = "more_address";
	$fields["mail_main"][] = "district_address";
	$fields["mail_main"][] = "postbox_address";
	#--
	//$fields["business_main"][] = "";
	#--
	//$fields["customer_main"][] = "";
	#--
	//$fields["tel_phone"][] = "";
	#--
	//$fields["tel_mobile"][] = "";
	#--
	//$fields["tel_fax"][] = "";
	#--
	//$fields["social_facebook"][] = "";
	#--
	//$fields["pay_paypal"][] = "";
	#--
	if (!$module)
	{
		return $modules;
	}
	else
	{
		if ($fields[$module])
		{
			return $fields[$module];
		}
		else
		{
			return false;
		}
	}
}

function wpud_get_alias($value, $domain = "wpud")
{
	#--
	$prefix = "wpud"."_";
	$value = $prefix.$value;
	#--
	if ($value == $prefix."profile_main") { return __("Profile Main", $domain);}
	else if ($value == $prefix."sex") { return __("Sex", $domain);}
	else if ($value == $prefix."birth_date") { return __("Date of Birth", $domain);}
	#--
	else if ($value == $prefix."mail_main") { return __("Mail Main", $domain);}
	else if ($value == $prefix."country") { return __("Country", $domain);}
	else if ($value == $prefix."region") { return __("Region", $domain);}
	else if ($value == $prefix."city") { return __("City", $domain);}
	else if ($value == $prefix."zipcode") { return __("Zipcode", $domain);}
	else if ($value == $prefix."full_address") { return __("Address Full", $domain);}
	else if ($value == $prefix."street_address") { return __("Street", $domain);}
	else if ($value == $prefix."number_address") { return __("Address Number", $domain);}
	else if ($value == $prefix."more_address") { return __("Address Complement", $domain);}
	else if ($value == $prefix."district_address") { return __("District", $domain);}
	else if ($value == $prefix."postbox_address") { return __("Post Office Box", $domain);}
	#--
	else if ($value == $prefix."business_main") { return __("Business Main", $domain);}
	#--
	else if ($value == $prefix."customer_main") { return __("Customer Main", $domain);}
	#--
	else if ($value == $prefix."tel_phone") { return __("Landline Phone", $domain);}
	#--
	else if ($value == $prefix."tel_mobile") { return __("Cell Phone", $domain);}
	#--
	else if ($value == $prefix."tel_fax") { return __("Phone Fax", $domain);}
	#--
	else if ($value == $prefix."social_facebook") { return __("Facebook Network", $domain);}
	#--
	else if ($value == $prefix."pay_paypal") { return __("Paypal Pay", $domain);}
	#--
	else { return $value; }
}

?>
